/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.io.Util;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.mr.qa.GlobalConfigs;
import com.mr.qa.exception.QaException;
import com.mr.qa.form.TestForm;

public class TestAction extends BaseAction
{
    private static final Log logger = LogFactory.getLog(TestAction.class);
    public ActionForward doAction(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) throws QaException
    {
        TestForm testForm = (TestForm)form;
        String phisicalFileDirStr = GlobalConfigs.WEB_PHISICAL_ROOT_LOCATION+"test"+File.separator;
        FormFile formFile = testForm.getFile();
        if(formFile != null)
        {
            logger.info("encoding:"+request.getCharacterEncoding()+","+formFile.getContentType());
        }
        else
            logger.info("encoding:"+request.getCharacterEncoding());
        try
        {
            String action = testForm.getAction();
            if("1".equals(action))
            {
                String fileName = formFile.getFileName();
                logger.info("fileName:"+new String(fileName.getBytes("iso-8859-1"),"gb2312"));
                logger.info("fileName:"+fileName);
                fileName = URLDecoder.decode(fileName,"UTF-8");
                logger.info("fileName:"+fileName);
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("2".equals(action))
            {
                String fileName = formFile.getFileName();
                fileName = new String(fileName.getBytes("iso-8859-1"),"gb2312");
                logger.info("fileName="+fileName);
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("3".equals(action))
            {
                String fileName = formFile.getFileName();
                testEncoding(fileName);
                String name = testForm.getName();
                logger.info("initial name="+name);
                logger.info("transfer name="+new String(name.getBytes("iso8859-1"),"GBK"));
                logger.info("fileName="+fileName);
                fileName = new String(fileName.getBytes("iso8859-1"),"gb2312");
                logger.info("transfer fileName="+fileName);
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("4".equals(action))
            {
                String fileName = formFile.getFileName();
                String name = testForm.getName();
                //fileName = URLDecoder.decode(fileName,"iso-8859-1");
                logger.info("fileName:"+fileName);
                testEncoding(fileName);
                logger.info("name:"+name);
                testEncoding(name);
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("5".equals(action))
            {
                String name = testForm.getName();
                testEncoding(name);
                logger.info(name);
                /*
                byte gbs[] = name.getBytes();//"gb2312"
                for(int i=0;i<gbs.length;i++)
                {
                    logger.info(gbs[i]);
                }*/
                //logger.info("fileName:"+.length);
            }
                 else if("6".equals(action))
                 {
                     String fileName = formFile.getFileName();
                     String name = testForm.getName();
                     testEncoding(name);
                     testEncoding(fileName);
                     fileName = new String(fileName.getBytes("GBK"),"UTF-8");
                     InputStream in = formFile.getInputStream();
                     String phisicalFilePath = phisicalFileDirStr+fileName;
                     OutputStream out = new FileOutputStream(phisicalFilePath);
                     Util.copyStream(in, out);
                     in.close();
                     out.close();
                 }
            else if("7".equals(action))
            {
                String fileName = formFile.getFileName();
                String name = testForm.getName();
                testEncoding(name);
                testEncoding(fileName);
                fileName = new String(fileName.getBytes("gb2312"),"iso8859-1");
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("8".equals(action))
            {
                String fileName = formFile.getFileName();
                //String name = testForm.getName();
                //testEncoding(name);
                //testEncoding(fileName);
                fileName = new String(fileName.getBytes("GBK"));
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("9".equals(action))
            {
                String fileName = formFile.getFileName();
                //String name = testForm.getName();
                //testEncoding(name);
                //testEncoding(fileName);
                fileName = new String(fileName.getBytes("gb2312"));
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("10".equals(action))
            {
                String fileName = formFile.getFileName();
                //String name = testForm.getName();
                //testEncoding(name);
                //testEncoding(fileName);
                fileName = new String(fileName.getBytes("gb2312"),"UTF-8");
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            else if("11".equals(action))
            {
                String fileName = formFile.getFileName();
                //String name = testForm.getName();
                //testEncoding(name);
                //testEncoding(fileName);
                fileName = MimeUtility.decodeText(fileName);
                InputStream in = formFile.getInputStream();
                String phisicalFilePath = phisicalFileDirStr+fileName;
                OutputStream out = new FileOutputStream(phisicalFilePath);
                Util.copyStream(in, out);
                in.close();
                out.close();
            }
            
        }
        catch(Exception e)
        {
            logger.error("test uploading file ex"+e);
        }
        
        return mapping.findForward("success");
    }
    
    private void testEncoding(String xx)
    {
        String[] encodings={"iso8859_1","gb2312","utf-8","windows-1252","GBK"};
        logger.info(xx);
        try
        {
            for(int i=0;i<encodings.length;i++)
            {
                if(xx.equals(new String(xx.getBytes(),encodings[i])))
                logger.info(encodings[i]);
            }
        }
        catch(Exception e){}
        
    }
    
    private String convertToGBK(String xx) throws UnsupportedEncodingException
    {
        byte[] xxBytes = xx.getBytes("GBK");
        String isoStr = new String(xxBytes,"GBK");
        xxBytes = xx.getBytes("ISO-8859-1");
        String gbkStr = new String(xxBytes,"GBK");
        logger.info(xx+"->gbkStr="+gbkStr);
        return gbkStr;
    }

    private String convertUTFToGBK(String xx) throws UnsupportedEncodingException
    {
        byte[] xxBytes = xx.getBytes("ISO-8859-1");
        String gbkStr = new String(xxBytes,"GBK");
        logger.info(xx+"->gbkStr="+gbkStr);
        return gbkStr;
    }
    
    
}
